package com.kingdee.pda.ui.pick_goods;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.basecommon.http.MyObserver;
import com.example.basecommon.http.RxHelper;
import com.example.basecommon.http.rxjava.BaseResponse;
import com.example.basecommon.http.rxjava.HttpUtils;
import com.example.basecommon.utils.ConstantUtil;
import com.example.basecommon.utils.SpeakUtils;
import com.kingdee.pda.Api;
import com.kingdee.pda.R;
import com.kingdee.pda.entity.Warehouse;
import com.kingdee.pda.ui.activity.BaseTitleActivity;
import com.kingdee.pda.ui.dialog.RemindDialog;
import com.kingdee.pda.util.CommonUtil;

import java.util.HashMap;

/**
 * PDA拣选
 * Created by dlj
 * DATE 2022/5/22
 */
public class PickGoodsOrderActivity extends BaseTitleActivity implements View.OnKeyListener {
    private EditText et_orderNo;
    private TextView tv_outOrderNo, tv_orderStatus, tv_skuTypes, tv_date;
    private RadioButton rb_pickNumber;
    private Button btn_confirm;
    private RelativeLayout rl_orderInfo;
    private PickOrderBean pickOrderBean;

    public static void actionStart(Context context) {
        context.startActivity(new Intent(context, PickGoodsOrderActivity.class));
    }

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        super.onActivityCreate(savedInstanceState);
        setContentView(R.layout.activity_pickgoods_order);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setTitle("PDA拣选");
        CommonUtil.requestFocusAndClearText(et_orderNo);
        rl_orderInfo.setVisibility(View.INVISIBLE);
        btn_confirm.setEnabled(false);
    }

    @Override
    protected void initView() {
        super.initView();

        et_orderNo = findViewById(R.id.et_orderNo);
        setEditTextScannerEvents(et_orderNo, this);

        tv_outOrderNo = findViewById(R.id.tv_outOrderNo);
        tv_orderStatus = findViewById(R.id.tv_orderStatus);
        tv_skuTypes = findViewById(R.id.tv_skuTypes);
        tv_date = findViewById(R.id.tv_date);

        rb_pickNumber = findViewById(R.id.rb_pickNumber);
        rl_orderInfo = findViewById(R.id.rl_orderInfo);
        btn_confirm = findViewById(R.id.btn_confirm);

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String number = et_orderNo.getText().toString();
                if (pickOrderBean != null && !TextUtils.isEmpty(number)) {
                    Intent intent = new Intent(PickGoodsOrderActivity.this, PickGoodsDetailActivity.class);
                    intent.putExtra("pickOrderBean", pickOrderBean);
                    intent.putExtra("pickType", rb_pickNumber.isChecked() ? 0 : 1);

                    startActivity(intent);
                } else {
                    SpeakUtils.speakWithToast(PickGoodsOrderActivity.this, "请先输入单号并确认！", true, false);
                    CommonUtil.requestFocusAndClearText(et_orderNo);
                }
            }
        });
    }

    public void scanPickNumber(String scanbillno) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("billNo", scanbillno);
        map.put("billType", "oewms_other_st_out");

        MyObserver<PickOrderBean> myObserver = new MyObserver<PickOrderBean>(this) {
            @Override
            public void onSuccess(BaseResponse<PickOrderBean> result) {
                SpeakUtils.onlyRing(PickGoodsOrderActivity.this, 2);
                btn_confirm.setEnabled(true);
                rl_orderInfo.setVisibility(View.VISIBLE);
                pickOrderBean = result.data;
                try {
                    tv_outOrderNo.setText(pickOrderBean.getBillNo());
                    if ("2".equals(pickOrderBean.getStockStatus())) {
                        tv_orderStatus.setText("待拣选");
                    } else if ("6".equals(pickOrderBean.getStockStatus())) {
                        tv_orderStatus.setText("拣选完成");
                    } else {
                        tv_orderStatus.setText(pickOrderBean.getStockStatus());
                    }
                    tv_skuTypes.setText(pickOrderBean.getSkuTypeQty() + "");
                    tv_date.setText(pickOrderBean.getCreateTime());

                } catch (Exception e) {
                    e.toString();
                }
            }

            @Override
            public void onFailure(BaseResponse result, String msg) {
                SpeakUtils.speakWithToast(PickGoodsOrderActivity.this, msg, true, false);
                btn_confirm.setEnabled(false);
                rl_orderInfo.setVisibility(View.INVISIBLE);
                CommonUtil.requestFocusAndClearText(et_orderNo);
                pickOrderBean = null;
            }
        };
        HttpUtils.request(Api.class)
                .pickQuery(ConstantUtil.PICK_QUERY, map)
                .compose(RxHelper.observableIO2Main(this))
                .subscribe(myObserver);
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (!(v instanceof EditText)) {
            return false;
        }
        EditText editText = (EditText) v;
        String input = editText.getText().toString().trim();
        if (TextUtils.isEmpty(input)) return false;
        if ((keyCode == KeyEvent.KEYCODE_SYSRQ || keyCode == KeyEvent.KEYCODE_ENTER) && event.getAction() == KeyEvent.ACTION_UP) {
            if (v.getId() == R.id.et_orderNo) {
                scanPickNumber(input);
                return true;
            }
        }
        return false;
    }


    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    public void changeWarehouse(Warehouse warehouse) {

    }
}
